<template>
  <Dialog v-model="dialogVisible" title="评价模板预览" width="900px" class="template-preview-dialog">
    <div v-loading="loading" class="template-preview">
      <!-- 模板标题 -->
      <div class="template-header">
        <h2 class="template-title">{{ templateData.templateName || '员工绩效评价表' }}</h2>
      </div>
      
      <!-- 基本信息 -->
      <div class="basic-info">
        <h3>基本信息：</h3>
        <div class="info-row">
          <span class="info-item">姓名：_______________</span>
          <span class="info-item">部门：_______________</span>
          <span class="info-item">岗位：_______________</span>
        </div>
        <div class="info-row">
          <span class="info-item">评价时间：___________</span>
          <span class="info-item">评价日期：___________</span>
          <span class="info-item">评价人：___________</span>
        </div>
      </div>
      
      <!-- 评价维度表格 -->
      <div class="evaluation-table">
        <table class="eval-table">
          <thead>
            <tr>
              <th class="dimension-col">评价维度</th>
              <th class="criteria-col">评价标准</th>
              <th class="score-col">得分</th>
            </tr>
          </thead>
          <tbody>
            <!-- 工作业绩 -->
            <tr class="dimension-row">
              <td rowspan="5" class="dimension-cell">
                1. 工作业绩 (25%)
              </td>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 优秀(5分)</span>
                  <span class="desc">- 超额完成目标</span>
                </div>
              </td>
              <td rowspan="5" class="score-cell">___/5分</td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 良好(4分)</span>
                  <span class="desc">- 完成既定目标</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 合格(3分)</span>
                  <span class="desc">- 基本完成目标</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 待改进(2分)</span>
                  <span class="desc">- 未完全完成</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 不合格(1分)</span>
                  <span class="desc">- 严重未达标</span>
                </div>
              </td>
            </tr>
            
            <!-- 工作质量 -->
            <tr class="dimension-row">
              <td rowspan="5" class="dimension-cell">
                2. 工作质量 (20%)
              </td>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 优秀(5分)</span>
                  <span class="desc">- 工作精准无误</span>
                </div>
              </td>
              <td rowspan="5" class="score-cell">___/5分</td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 良好(4分)</span>
                  <span class="desc">- 质量符合要求</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 合格(3分)</span>
                  <span class="desc">- 质量符合要求</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 待改进(2分)</span>
                  <span class="desc">- 偶有质量问题</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 不合格(1分)</span>
                  <span class="desc">- 质量问题较多</span>
                </div>
              </td>
            </tr>
            
            <!-- 工作态度 -->
            <tr class="dimension-row">
              <td rowspan="5" class="dimension-cell">
                3. 工作态度 (20%)
              </td>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 优秀(5分)</span>
                  <span class="desc">- 积极主动负责</span>
                </div>
              </td>
              <td rowspan="5" class="score-cell">___/5分</td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 良好(4分)</span>
                  <span class="desc">- 认真负责</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 合格(3分)</span>
                  <span class="desc">- 态度端正</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 待改进(2分)</span>
                  <span class="desc">- 态度一般</span>
                </div>
              </td>
            </tr>
            <tr>
              <td class="criteria-cell">
                <div class="criteria-item">
                  <span class="level">□ 不合格(1分)</span>
                  <span class="desc">- 态度消极</span>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      
      <!-- 模板信息 -->
      <div class="template-meta-info">
        <el-descriptions :column="3" border>
          <el-descriptions-item label="模板编码">
            {{ templateData.templateCode || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="适用职业">
            {{ templateData.applicableProfession || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="适用等级">
            {{ templateData.applicableLevel || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="评价方式">
            <dict-tag :type="DICT_TYPE.EVAL_EVALUATION_METHOD" :value="templateData.applicableMethod" />
          </el-descriptions-item>
          <el-descriptions-item label="合格分数">
            {{ templateData.passScore || 0 }}分
          </el-descriptions-item>
          <el-descriptions-item label="满分">
            {{ templateData.totalScore || 0 }}分
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
    
    <template #footer>
      <el-button @click="dialogVisible = false">关闭</el-button>
      <el-button type="primary" @click="handlePrint">打印预览</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { DICT_TYPE } from '@/utils/dict'
import { getTemplate } from '@/api/eval/template'

const dialogVisible = ref(false)
const loading = ref(false)
const templateData = ref({} as any)

const open = async (id: number) => {
  dialogVisible.value = true
  loading.value = true
  
  try {
    const data = await getTemplate(id)
    templateData.value = data
  } catch (error) {
    console.error('获取模板详情失败:', error)
  } finally {
    loading.value = false
  }
}

const handlePrint = () => {
  window.print()
}

defineExpose({ open })
</script>

<style scoped>
.template-preview {
  font-family: 'Microsoft YaHei', sans-serif;
  line-height: 1.6;
}

.template-header {
  text-align: center;
  margin-bottom: 30px;
  border-bottom: 3px solid #000;
  padding-bottom: 10px;
}

.template-title {
  font-size: 24px;
  font-weight: bold;
  margin: 0;
}

.basic-info {
  margin-bottom: 30px;
}

.basic-info h3 {
  margin: 0 0 15px 0;
  font-size: 16px;
  font-weight: bold;
}

.info-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.info-item {
  flex: 1;
  margin-right: 20px;
}

.evaluation-table {
  margin-bottom: 30px;
}

.eval-table {
  width: 100%;
  border-collapse: collapse;
  border: 2px solid #000;
}

.eval-table th,
.eval-table td {
  border: 1px solid #000;
  padding: 8px;
  text-align: left;
  vertical-align: top;
}

.eval-table th {
  background-color: #f5f5f5;
  font-weight: bold;
  text-align: center;
}

.dimension-col {
  width: 120px;
}

.criteria-col {
  width: auto;
}

.score-col {
  width: 80px;
}

.dimension-cell {
  font-weight: bold;
  text-align: center;
  vertical-align: middle;
  background-color: #fafafa;
}

.criteria-cell {
  padding: 5px 8px;
}

.criteria-item {
  display: flex;
  align-items: center;
  margin-bottom: 2px;
}

.criteria-item:last-child {
  margin-bottom: 0;
}

.level {
  min-width: 80px;
  font-weight: bold;
}

.desc {
  margin-left: 10px;
  color: #666;
}

.score-cell {
  text-align: center;
  vertical-align: middle;
  font-weight: bold;
}

.template-meta-info {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

/* 打印样式 */
@media print {
  .template-preview-dialog :deep(.el-dialog__header),
  .template-preview-dialog :deep(.el-dialog__footer) {
    display: none !important;
  }
  
  .template-preview {
    margin: 0;
    padding: 20px;
  }
  
  .eval-table {
    page-break-inside: avoid;
  }
}
</style>
